# -*- coding: utf-8 -*-
"""
Created on Thu Mar 13 12:00:16 2025

@author: usuario
"""

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
from scipy.signal import savgol_filter

from AuxiliaryFunctions import (Ic_and_efficiency_diode_1file_V2,
                                plot_IVs_several_B)
from scipy.optimize import curve_fit
def Icp_Icm_model(B, Ic, dIc, a, b):
    y1 = np.min(np.array([Ic + a * B + b, Ic + dIc - a * B + b]), axis=0)# Icp
    y2 = np.min(np.array([Ic - a * B - b, Ic + dIc + a * B - b]), axis=0)# abs(Icm)
    return y1, y2
def Icp_fun(B, Ic, dIc, a, b):
    y1 = np.min(np.array([Ic + a * B + b, Ic + dIc - a * B + b]), axis=0)
    return y1
font = {'family' : 'DejaVu Sans',
        'weight' : 'normal',
        'size'   : '15'}
plt.rc('font', **font)
path = ".\\DataSingleDiode\\"

#%%
B0 = 1
Vran = []#[-0.05, 0.05]
filename = 'M13_VPT0112_Ch1_IV_I-V-_Imax2.0mA_Bz-m10to50Oe_1p7K_50KCooledm1kOeAfter1kOe_Keithley2450.dat'
B, Icp, Icm = Ic_and_efficiency_diode_1file_V2(path+filename, plot_IV_curve=False, B_dir='$\\mu_0H_z$ (mT)',
                              B0=B0, Bran=[-2,2], Vran=Vran, title='M13 D1: 1V-1I-',
                              rev=True, rs=0.4, Ics=True, efficiency=False)#-100, 100
#plot_IVs_several_B(filename, Blist=[5, 15], B0=B0, Vran=[])#-0.15, 0.15
Ic, dIc, a, b = 1.14+0.15, 1.11, 0.11*10, 0.28+0.05 #1.3, 0.9, 0.1, 0.3
plt.ylim([0.25, 2.3])
filt_B2, filt_B1 = np.multiply(B > -1, B < 0.2), np.multiply(B > -0.85, B < 1.5)
y1, y2 = Icp_Icm_model(B, Ic, dIc, a, b)
plt.plot(B[filt_B1], y1[filt_B1], '--k')
plt.plot(B[filt_B2], y2[filt_B2], '--k')
plt.savefig('M13_Diode1Efficiency.pdf')
plt.show()
#%% Second diode 1I+to 1V+
B0 = 1.05
Vran = [-0.51, 0.45]#[-0.05, 0.05]
filename = 'M14_VPT0112_Ch1_IV_I+V+_Imax2.5mA_Bz-m10to50Oe_1p7K_50KCooledm1kOeAfter1kOe_Keithley2450.dat'
B, Icp, Icm = Ic_and_efficiency_diode_1file_V2(path+filename, plot_IV_curve=False, B_dir='$\\mu_0H_z$ (mT)',
                              B0=B0, Bran=[-2, 2], Vran=Vran, title='M14 D2: 1V+1I+',
                              rev=True, rs=0.5, Ics=True, efficiency=False)#-100, 100
#plot_IVs_several_B(filename, Blist=[5, 16], B0=B0, Vran=[-0.15, 0.15])
Ic, dIc, a, b = 1.14 + 0.25, 1.11, 0.11*10, 0.28 #1.3, 0.9, 0.1, 0.3
plt.ylim([0.25, 2.3])
filt_B2, filt_B1 = np.multiply(B > -1, B < 0.2), np.multiply(B > -0.85, B < 1.5)
y1, y2 = Icp_Icm_model(B, Ic, dIc, a, b)
plt.plot(B[filt_B1], y1[filt_B1], '--k')
plt.plot(B[filt_B2], y2[filt_B2], '--k')
plt.savefig('M14_Diode2Efficiency.pdf')
plt.show()
#%% Fourth diode 2I+to 2I-
B0 = 1.1
Vran = [-0.4, 0.5]#[-0.05, 0.05]
rev = True
filename = 'M15_VPT0112_Ch2_IV_I+I-_Imax2.5mA_Bz-m10to50Oe_1p7K_After1kOe_Keithley2450.dat'
B, Icp, Icm = Ic_and_efficiency_diode_1file_V2(path+filename, plot_IV_curve=False, B_dir='$\\mu_0H_z$ (mT)',
                              B0=B0, Bran=[-2, 2], Vran=Vran, title='M15 D4: 2I+ to 2I-',
                              rs=0.4, rev=False, Ics=True, efficiency=False)#-100, 100
plt.ylim([0.25, 2.3])
#plt.show()
#ax2.set_ylim([-50, 65])

filt_B = np.multiply(B < 15, B > -10)
popt, pcov = curve_fit(Icp_fun, xdata=B[filt_B],
                       ydata=Icp[filt_B], p0=(1.9, 5, 1, 0.28),
                       bounds=([0, 0, 0.1, 0.27], [1e6, 1e3, 1e6, 0.3]))
#plt.plot(B[Bfilt], Icp_fun(B[Bfilt], *popt))
# model
Ic, dIc, a, b = 1.14, 1.11, 0.11*10, 0.28 #1.3, 0.9, 0.1, 0.3
filt_B2, filt_B1 = np.multiply(B > -1, B < 0.2), np.multiply(B > -0.85, B < 1.5)
y1, y2 = Icp_Icm_model(B, Ic, dIc, a, b)
plt.plot(B[filt_B1], y1[filt_B1], '--k')
plt.plot(B[filt_B2], y2[filt_B2], '--k')

plt.savefig('M15_Diode4Efficiency.pdf')
plt.show()
#%% Third diode 2V+to 2V-
B0 = 0.77
Vran = [-0.4, 0.45]#[-0.05, 0.05]
filename = 'M16_VPT0112_Ch2_IV_V+V-_Imax2.5mA_Bz-m10to50Oe_1p7K_After1kOe_Keithley2450.dat'
B, Icp, Icm = Ic_and_efficiency_diode_1file_V2(path+filename, plot_IV_curve=False, B_dir='$\\mu_0H_z$ (mT)',
                              B0=B0, Bran=[-2, 2], Vran=Vran, title='M16 D3: 2V+ to 2V-',
                              rs=0.4, rev=False, Ics=True, efficiency=False)#-100, 100
plt.ylim([0.25, 2.3])
#ax2.set_ylim([3, 65])
# model
Ic, dIc, a, b = 1.14, 1.11, 0.11*10, 0.28 #1.3, 0.9, 0.1, 0.3
filt_B2, filt_B1 = np.multiply(B > -1, B < 0.35), np.multiply(B > -0.9, B < 1.5)
y1, y2 = Icp_Icm_model(B, Ic, dIc, a, b)
plt.plot(B[filt_B1], y1[filt_B1], '--k')
plt.plot(B[filt_B2], y2[filt_B2], '--k')
plt.savefig('M16_Diode3Efficiency.pdf')
plt.show()
#plot_IVs_several_B(filename, Blist=[4, 14], B0=B0, Vran=[], rev=True)
#%% Third diode 2V+to 2V- After Bx=-1 kOe
B0 = 0.7
Vran = [-0.5, 0.45]#[-0.05, 0.05]
filename = 'M17_VPT0112_Ch2_IV_V+V-_Imax2.5mA_Bz-m10to50Oe_1p7K_Afterm1kOe_Keithley2450.dat'
B, Icp, Icm = Ic_and_efficiency_diode_1file_V2(path+filename, plot_IV_curve=False, B_dir='$\\mu_0H_z$ (mT)',
                              B0=B0, Bran=[-2, 2], Vran=Vran, title='M17 D3: 2V+ to 2V-',
                              rs=0.4, rev=False, Ics=True, efficiency=False)#-100, 100
plt.ylim([0.25, 2.3])
# model
Ic, dIc, a, b = 1.14, 1.11, 0.11*10, -0.28 #1.3, 0.9, 0.1, 0.3
filt_B2, filt_B1 = np.multiply(B > -1.5, B < 0.75), np.multiply(B > -0.1, B < 1)
y1, y2 = Icp_Icm_model(B, Ic, dIc, a, b)
plt.plot(B[filt_B1], y1[filt_B1], '--k')
plt.plot(B[filt_B2], y2[filt_B2], '--k')
plt.savefig('M17_Diode3Efficiency.pdf')
plt.show()
plt.figure(figsize=[4.5, 3.67])
plt.plot([0,0],[-2, 2.2], 'gray')
plt.plot([-0.5, 0.5],[0, 0], 'gray')
plt.xlim([-0.4, 0.4])
plt.ylim([-2, 2])

plot_IVs_several_B(path+filename, Blist=[2], B0=B0, Vran=[-0.35, 0.41], rev=False)
plt.minorticks_on()
plt.title('$\\mu_0H_z=-$0.5 mT')
plt.tick_params(which='both', direction='in', top=True, right=True)
plt.savefig('IV_SD3_Bzm5Oe.pdf')
plt.show()
#%% D4: 2I+to 2I- After Bx=-1 kOe
B0 = 0.675
Vran = [-0.5, 0.5]#[-0.05, 0.05]
filename = 'M18_VPT0112_Ch2_IV_I+I-_Imax2.5mA_Bz-m10to50Oe_1p7K_Afterm1kOe_Keithley2450.dat'
Ic_and_efficiency_diode_1file_V2(path+filename, plot_IV_curve=False, B_dir='$\\mu_0H_z$ (mT)',
                              B0=B0, Bran=[-2, 2], Vran=Vran, title='M18 D4: 2I+ to 2I-',
                              rs=0.4, rev=False, efficiency=False)#-100, 100
plt.ylim([0.25, 2.3])
# model
Ic, dIc, a, b = 1.14, 1.11, 0.11*10, -0.28 #1.3, 0.9, 0.1, 0.3
filt_B2, filt_B1 = np.multiply(B > -1.7, B < 1.0), np.multiply(B > -0.5, B < 1.0)
y1, y2 = Icp_Icm_model(B, Ic, dIc, a, b)
plt.plot(B[filt_B1], y1[filt_B1], '--k')
plt.plot(B[filt_B2], y2[filt_B2], '--k')
plt.savefig('M18_Diode4Efficiency.pdf')
plt.show()
#plot_IVs_several_B(filename, Blist=[4, 14], B0=B0, Vran=[], rev=True)
#%% D2: 1I+1V+ After Bx=-1 kOe
B0 = 0.675
Vran = [-0.5, 0.5]#[-0.05, 0.05]
filename = 'M19_VPT0112_Ch1_IV_I+V+_Imax2.5mA_Bz-m10to50Oe_1p7K_Afterm1kOe_Keithley2450.dat'
Ic_and_efficiency_diode_1file_V2(path+filename, plot_IV_curve=False, B_dir='$\\mu_0H_z$ (mT)',
                              B0=B0, Bran=[-2, 2], Vran=Vran, rev=True,
                              title='M19 D2: 1V+1I+', rs=0.5, efficiency=False)#-100, 100
# ax1.set_ylim([0.25, 2.3])
#plot_IVs_several_B(filename, Blist=[B0 - 6, B0 + 6], B0=B0, Vran=[])
Ic, dIc, a, b = 1.14 + 0.25, 1.11, 0.11*10, -0.28 #1.3, 0.9, 0.1, 0.3
filt_B2, filt_B1 = np.multiply(B > -1.7, B < 1.0), np.multiply(B > -0.5, B < 1.0)
y1, y2 = Icp_Icm_model(B, Ic, dIc, a, b)
plt.plot(B[filt_B1], y1[filt_B1], '--k')
plt.plot(B[filt_B2], y2[filt_B2], '--k')
plt.savefig('M19_Diode2Efficiency.pdf')
plt.show()
#%% D1: 1I-1V- After Cooling100K90 deg B=0
B0 = 0.725
Vran = [-0.5, 0.5]#[-0.05, 0.05]
filename = 'M20_VPT0112_Ch1_IV_I-V-_Imax2.5mA_Bz-m10to50Oe_1p7K_Afterm1kOe_Keithley2450.dat'
Ic_and_efficiency_diode_1file_V2(path+filename, plot_IV_curve=False, B_dir='$\\mu_0H_z$ (mT)',
                              B0=B0, Bran=[-2, 2], Vran=Vran, title='M20 D1: 1V-1I-',
                              rs=0.4, rev=True, efficiency=False)
Ic, dIc, a, b =  1.14+0.15, 1.11, 0.11*10, -(0.28+0.05) 
filt_B2, filt_B1 = np.multiply(B > -1.7, B < 1.0), np.multiply(B > -0.5, B < 1.0)
y1, y2 = Icp_Icm_model(B, Ic, dIc, a, b)
plt.plot(B[filt_B1], y1[filt_B1], '--k')
plt.plot(B[filt_B2], y2[filt_B2], '--k')
# ax1.set_ylim([0.25, 2.3])
plt.savefig('M20_Diode1Efficiency.pdf')
plt.show()